using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using FytSoa.Application.Sys.Dto;
using FytSoa.Common;
using FytSoa.Model.Sys;
using FytSoa.Repository.Interfaces;

namespace FytSoa.Application.Sys
{
    public class SysCodeTypeService : ISysCodeTypeService
    {
        private readonly IBaseRepository<SysCodeType> _thisRepository;
        public SysCodeTypeService(IBaseRepository<SysCodeType> thisRepository)
        {
            _thisRepository = thisRepository;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<SysCodeTypeDto>>> List()
        {
            var result = new ApiResult<List<SysCodeTypeDto>>();
            try
            {
                var list = new List<SysCodeTypeDto>();
                //查询所有
                var reslist = await _thisRepository.GetListAsync(m=>!m.IsDel, m => m.Sort, 1);
                //转换到dto
                var listDto = reslist.MapToList<SysCodeType, SysCodeTypeDto>();
                foreach (var item in listDto.Where(m => m.ParentId == "0").OrderByDescending(m => m.Sort))
                {
                    item.children = GetChild(new List<SysCodeTypeDto>(), listDto, item.Id);
                    list.Add(item);
                }
                result.Data = list;
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 递归形成tree
        /// </summary>
        /// <returns></returns>
        private List<SysCodeTypeDto> GetChild(List<SysCodeTypeDto> resList, List<SysCodeTypeDto> sourceList, string id)
        {
            var child = sourceList.Where(m => m.ParentId == id).OrderByDescending(m => m.Sort).ToList();
            foreach (var item in child)
            {
                item.children = GetChild(new List<SysCodeTypeDto>(), sourceList, item.Id);
                resList.Add(item);
            }
            return resList;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Add(SysCodeTypeParam model)
        {
            var result = new ApiResult<string>();
            try
            {
                model.Id = Unique.Id();
                var typeModel = model.MapTo<SysCodeTypeParam, SysCodeType>();
                typeModel.Layer = model.ParentId == "0" ? 1 : 2;
                typeModel.CreateUser = "admin";
                await _thisRepository.AddAsync(typeModel);

            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Modify(SysCodeTypeParam model)
        {
            var result = new ApiResult<string>();
            try
            {
                var typeModel = model.MapTo<SysCodeTypeParam, SysCodeType>();
                typeModel.Layer = model.ParentId == "0" ? 1 : 2;
                await _thisRepository.UpdateAsync(typeModel, m => new { m.CreateUser, m.CreateTime, m.IsDel });
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 查询实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<SysCodeType>> GetModel(string id)
        {
            var result = new ApiResult<SysCodeType>();
            try
            {
                await _thisRepository.GetModelAsync(m=>m.Id==id);
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Delete(List<string> ids)
        {
            var result = new ApiResult<string>();
            try
            {
                //假删除
                await _thisRepository.UpdateAsync(m=>new SysCodeType() { IsDel=true}, m => ids.Contains(m.Id));
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

    }
}
